package com.yiqi.liebang.common.widget.filter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;

import com.baiiu.filter.ClassifyBo;
import com.baiiu.filter.adapter.MenuAdapter;
import com.baiiu.filter.adapter.SimpleTextAdapter;
import com.baiiu.filter.adapter.SimpleTextAdapter2;
import com.baiiu.filter.interfaces.OnFilterDoneListener;
import com.baiiu.filter.interfaces.OnFilterItemClickListener;
import com.baiiu.filter.typeview.DoubleListView;
import com.baiiu.filter.typeview.SingleListView;
import com.baiiu.filter.util.CommonUtil;
import com.baiiu.filter.util.UIUtil;
import com.baiiu.filter.view.FilterCheckedTextView;
import com.baiiu.filter.view.FilterCheckedTextView2;
import com.suozhang.framework.component.http.RxDataProcessFactory;
import com.suozhang.framework.framework.AM;
import com.suozhang.framework.utils.T;
import com.yiqi.liebang.R;
import com.yiqi.liebang.common.widget.filter.entity.FilterType;
import com.yiqi.liebang.common.widget.filter.entity.FilterUrl;
import com.yiqi.liebang.framework.api.HomeApi;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * author: baiiu
 * date: on 16/1/17 21:14
 * description:
 */
public class DropMenuAdapter implements MenuAdapter {
    private final Context mContext;
    private OnFilterDoneListener onFilterDoneListener;
    private String[] titles;
    boolean show = true;
    String mClassify2Id;
    String mClassifyId;

    public DropMenuAdapter(Context context, String[] titles,String mClassifyId ,String mClassify2Id, OnFilterDoneListener onFilterDoneListener, boolean show) {
        this.mContext = context;
        this.titles = titles;
        this.show = show;
        this.mClassifyId=mClassifyId;
        this.mClassify2Id = mClassify2Id;
        this.onFilterDoneListener = onFilterDoneListener;

    }

    @Override
    public int getMenuCount() {
        return titles.length;
    }

    @Override
    public String getMenuTitle(int position) {
        return titles[position];
    }

    @Override
    public int getBottomMargin(int position) {
        if (position == 3) {
            return 0;
        }

        return UIUtil.dp(mContext, 140);
    }

    @Override
    public View getView(int position, FrameLayout parentContainer) {
        View view = parentContainer.getChildAt(position);

        switch (position) {
            case 0:
                view = createDoubleListView();
                break;
            case 1:
                if (show) {
                    view = createServiceTypeList();
                } else {
                    view = createSortList();
                }
                break;
            case 2:
                view = createSortList();
                break;

        }

        return view;
    }

    private View createServiceTypeList() {
        SingleListView<ClassifyBo> singleListView = new SingleListView<ClassifyBo>(mContext)
                .adapter(new SimpleTextAdapter<ClassifyBo>(null, mContext) {
                    @Override
                    public ClassifyBo provideText(ClassifyBo string) {
                        return string;
                    }

                    @Override
                    protected void initCheckedTextView(FilterCheckedTextView checkedTextView) {
                        int dp = UIUtil.dp(mContext, 15);
                        checkedTextView.setPadding(dp, dp, 60, dp);

                    }
                })
                .onItemClick(new OnFilterItemClickListener<ClassifyBo>() {
                    @Override
                    public void onItemClick(ClassifyBo item) {
                        FilterUrl.instance().position = 1;
                        FilterUrl.instance().singleServicePosition = item;
                        FilterUrl.instance().positionTitle = item;
                        onFilterDone();
                    }
                });

        List<ClassifyBo> list = new ArrayList<>();
        list.add(new ClassifyBo(null, "全部"));
        list.add(new ClassifyBo("1", "全国通话"));
        list.add(new ClassifyBo("0", "线下约见"));
        singleListView.setList(list, 0);

        return singleListView;
    }

    private View createSortList() {






        SingleListView<ClassifyBo> singleListView2 = new SingleListView<ClassifyBo>(mContext)
                .adapter(new SimpleTextAdapter<ClassifyBo>(null, mContext) {
                    @Override
                    public ClassifyBo provideText(ClassifyBo string) {
                        return string;
                    }

                    @Override
                    protected void initCheckedTextView(FilterCheckedTextView checkedTextView) {
                        int dp = UIUtil.dp(mContext, 13);
                        checkedTextView.setPadding(dp, dp, 60, dp);
                        if (checkedTextView.isChecked()){
                            Drawable drawableLeft = mContext.getResources().getDrawable(
                                    R.drawable.list_button_xuanze);
                            checkedTextView.setCompoundDrawablesWithIntrinsicBounds(null,
                                    null, drawableLeft, null);
                            checkedTextView.setCompoundDrawablePadding(4);
                        }
                    }
                })
                .onItemClick(new OnFilterItemClickListener<ClassifyBo>() {
                    @Override
                    public void onItemClick(ClassifyBo item) {
                        FilterUrl.instance().position = show ? 2 : 1;
                        FilterUrl.instance().singleSortPosition = item;
                        FilterUrl.instance().positionTitle = item;
                        onFilterDone();
                    }
                });

        List<ClassifyBo> list = new ArrayList<>();
        list.add(new ClassifyBo("1", "帮助最多"));
        list.add(new ClassifyBo("2", "评分最高"));
        list.add(new ClassifyBo("3", "价格最低"));
        singleListView2.setList(list, -1);

        return singleListView2;
    }

    private View createDoubleListView() {
        DoubleListView<FilterType, ClassifyBo> comTypeDoubleListView = new DoubleListView<FilterType, ClassifyBo>(mContext)
                .leftAdapter(new SimpleTextAdapter2<FilterType>(null, mContext) {
                    @Override
                    public ClassifyBo provideText(FilterType filterType) {
                        return filterType.desc;
                    }

                    @Override
                    protected void initCheckedTextView(FilterCheckedTextView2 checkedTextView) {
                        checkedTextView.setPadding(UIUtil.dp(mContext, 13), UIUtil.dp(mContext, 15), 0, UIUtil.dp(mContext, 15));
//                        checkedTextView.setBackgroundResource(R.color.color_ee_f2_f5);

                    }
                })
                .rightAdapter(new SimpleTextAdapter<ClassifyBo>(null, mContext) {
                    @Override
                    public ClassifyBo provideText(ClassifyBo s) {
                        return s;
                    }

                    @Override
                    protected void initCheckedTextView(FilterCheckedTextView checkedTextView) {
                        checkedTextView.setPadding(UIUtil.dp(mContext, 13), UIUtil.dp(mContext, 15), 60, UIUtil.dp(mContext, 15));
                    }
                })
                .onLeftItemClickListener(new DoubleListView.OnLeftItemClickListener<FilterType, ClassifyBo>() {
                    @Override
                    public List<ClassifyBo> provideRightList(FilterType item, int position) {
                        List<ClassifyBo> child = item.child;
                        if (CommonUtil.isEmpty(child)) {
                            FilterUrl.instance().position = 0;
                            FilterUrl.instance().doubleListLeft = item.desc;
                            FilterUrl.instance().doubleListRight = null;
                            FilterUrl.instance().positionTitle = item.desc;

                            onFilterDone();
                        }

                        return child;
                    }
                })

                .onRightItemClickListener(new DoubleListView.OnRightItemClickListener<FilterType, ClassifyBo>() {
                    @Override
                    public void onRightItemClick(FilterType item, ClassifyBo string) {
                        FilterUrl.instance().position = 0;
                        FilterUrl.instance().doubleListLeft = item.desc;
                        FilterUrl.instance().doubleListRight = string;
                        FilterUrl.instance().positionTitle = string;
                        onFilterDone();
                    }
                });


        List<FilterType> list = new ArrayList<>();

        AM.api().createApiService(HomeApi.class)
                .getAllClassify()
                .compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer())
                .subscribe(new Observer<List<ClassifyBo>>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(List<ClassifyBo> classifyBos) {
                for (int i = 0; i < classifyBos.size(); i++) {
                    list.add(new FilterType(classifyBos.get(i), classifyBos.get(i).getClassifyTwo()));
                    //初始化选中.
                }
                for (int i = 0; i < list.size(); i++) {
                    list.get(i).child.add(0, new ClassifyBo(null, "全部"));
                }
                int leftPos = 0;
                int rightPos = 0;
                if (!TextUtils.isEmpty(mClassify2Id)) {
                    for (int i = 0; i < list.size(); i++) {
                        for (int j = 0; j < list.get(i).child.size(); j++) {
                            if (mClassify2Id.equals(list.get(i).child.get(j).getId())) {
                                leftPos = i;
                                rightPos = j;
                            }
                        }
                    }
                }else {
                    for (int i = 0; i < list.size(); i++) {
                        if (!TextUtils.isEmpty(mClassifyId)){
                            if (mClassifyId.equals(list.get(i).desc.getId())) {
                                leftPos = i;
                            }
                        }

                    }
                }


                //初始化选中.
                comTypeDoubleListView.setLeftList(list, leftPos);
                comTypeDoubleListView.setRightList(list.get(leftPos).child, rightPos);
                comTypeDoubleListView.getLeftListView().setBackgroundColor(mContext.getResources().getColor(R.color.b_c_fafafa));
            }

            @Override
            public void onError(Throwable e) {
                T.showShort(e.getMessage());
            }

            @Override
            public void onComplete() {

            }
        });


        return comTypeDoubleListView;
    }


    private void onFilterDone() {
        if (onFilterDoneListener != null) {
            onFilterDoneListener.onFilterDone(0, "", "");
        }
    }

}
